package com.lmscn.lms.api.util;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.lmscn.lms.api.model.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Czl on 2015/8/10.
 */
public class BuildPaperUtil {

    @Before(Tx.class)
    public static Map BuildExamPaper(Exam exam, Long studentId) {
        Map<String, Object> dataMapList = new HashMap<String, Object>();
        List list = new ArrayList();
        //获取单选题目数量
        List singleList = Question.dao.getQuestions("single_choice", exam.getInt("rand_single_num"), exam.getStr("rand_categery_id"));
        //获取多选题目数量
        List multiList = Question.dao.getQuestions("multi_choice", exam.getInt("rand_multi_num"), exam.getStr("rand_categery_id"));
        //获取判断题目数量
        List determineList = Question.dao.getQuestions("determine_choose", exam.getInt("rand_choose_num"), exam.getStr("rand_categery_id"));
        List questionList = new ArrayList();
        questionList.addAll(singleList);
        questionList.addAll(multiList);
        questionList.addAll(determineList);
        //获取设定的考试总题目数
        int questionCount = exam.getInt("rand_single_num") + exam.getInt("rand_multi_num") + exam.getInt("rand_choose_num");
        //获取实际从题库中获得的题目数。由于题库中符合条件的题目数量不够，可能出现实际生成的题目达不到试卷题目数的要求。
        int realQuestionCount = questionList.size();
        if (realQuestionCount < questionCount) {
            //获得除考试选定题库外的其它题库。
            List otherQuestionCategory = QuestionCategory.dao.getOtherQuestionCategory(exam.getStr("rand_categery_id"));
            //把其它题库转换为String
            String otherCategory = "";
            for (int m = 0; m < otherQuestionCategory.size(); m++) {
                QuestionCategory questionCategory = (QuestionCategory) otherQuestionCategory.get(m);
                otherCategory += questionCategory.get("id") + ",";
            }
            //如果其它题库不为空，则从该题库中提取试题
            if (otherCategory.length() > 0) {
                otherCategory = otherCategory.substring(0, otherCategory.length() - 1);
                //比较各类题型的题目实际获得数是否和考试设定值一致，如果不一致，则从其它题库中提取
                if (singleList.size() < exam.getInt("rand_single_num")) {
                    List OtherSingleList = Question.dao.getQuestions("single_choice", exam.getInt("rand_single_num") - singleList.size(), otherCategory);
                    questionList.addAll(OtherSingleList);
                }
                if (multiList.size() < exam.getInt("rand_multi_num")) {
                    List OtherMultiList = Question.dao.getQuestions("multi_choice", exam.getInt("rand_multi_num") - multiList.size(), otherCategory);
                    questionList.addAll(OtherMultiList);
                }
                if (determineList.size() < exam.getInt("rand_choose_num")) {
                    List OtherDetermineList = Question.dao.getQuestions("determine_choose", exam.getInt("rand_choose_num") - determineList.size(), otherCategory);
                    questionList.addAll(OtherDetermineList);
                }

            }
        }
        //把选项赋给题目
        for (int i = 0; i < questionList.size(); i++) {
            Question question = (Question) questionList.get(i);
            List questionMetasList = QuestionMetas.dao.getQuestionMetas(Long.valueOf(question.getBigInteger("id").toString()));
            for (int j = 0; j < questionMetasList.size(); j++) {
                QuestionMetas questionMetas = (QuestionMetas) questionMetasList.get(j);
                question.set("item" + (j + 1) + "_id", questionMetas.get("options"));
            }
            list.add(question);
        }
        List nullList = new ArrayList();
        Map<String, Object> questionsMap = new HashMap<String, Object>();
        questionsMap.put("question", questionList);
        questionsMap.put("answered", nullList);
        dataMapList.put("questions", questionsMap);
        // 题目保存
        Testpaper.dao.saveTestPaper(exam, questionList, studentId);
        return dataMapList;
    }

    @Before(Tx.class)
    public static Map BuildTestPaper(Test test, Long studentId) {
        Map<String, Object> dataMapList = new HashMap<String, Object>();
        List testList = new ArrayList();
        //获取单选题目数量
        List singleList = Question.dao.getQuestions("single_choice", test.getInt("rand_single_num"), test.getStr("rand_categery_id"));
        //获取多选题目数量
        List multiList = Question.dao.getQuestions("multi_choice", test.getInt("rand_multi_num"), test.getStr("rand_categery_id"));
        //获取判断题目数量
        List determineList = Question.dao.getQuestions("determine_choose", test.getInt("rand_choose_num"), test.getStr("rand_categery_id"));
        List questionList = new ArrayList();
        questionList.addAll(singleList);
        questionList.addAll(multiList);
        questionList.addAll(determineList);
        //获取设定的考试总题目数
        int questionCount = test.getInt("rand_single_num") + test.getInt("rand_multi_num") + test.getInt("rand_choose_num");
        //获取实际从题库中获得的题目数。由于题库中符合条件的题目数量不够，可能出现实际生成的题目达不到试卷题目数的要求。
        int realQuestionCount = questionList.size();
        if (realQuestionCount < questionCount) {
            //获得除考试选定题库外的其它题库。
            List otherQuestionCategory = QuestionCategory.dao.getOtherQuestionCategory(test.getStr("rand_categery_id"));
            //把其它题库转换为String
            String otherCategory = "";
            for (int m = 0; m < otherQuestionCategory.size(); m++) {
                QuestionCategory questionCategory = (QuestionCategory) otherQuestionCategory.get(m);
                otherCategory += questionCategory.get("id").toString() + ",";
            }
            //如果其它题库不为空，则从该题库中提取试题
            if (otherCategory.length() > 0) {
                otherCategory = otherCategory.substring(0, otherCategory.length() - 1);
                //比较各类题型的题目实际获得数是否和考试设定值一致，如果不一致，则从其它题库中提取
                if (singleList.size() < test.getInt("rand_single_num")) {
                    List OtherSingleList = Question.dao.getQuestions("single_choice", test.getInt("rand_single_num") - singleList.size(), otherCategory);
                    questionList.addAll(OtherSingleList);
                }
                if (multiList.size() < test.getInt("rand_multi_num")) {
                    List OtherMultiList = Question.dao.getQuestions("multi_choice", test.getInt("rand_multi_num") - multiList.size(), otherCategory);
                    questionList.addAll(OtherMultiList);
                }
                if (determineList.size() < test.getInt("rand_choose_num")) {
                    List OtherDetermineList = Question.dao.getQuestions("determine_choose", test.getInt("rand_choose_num") - determineList.size(), otherCategory);
                    questionList.addAll(OtherDetermineList);
                }

            }
        }
        //把选项赋给题目
        for (int i = 0; i < questionList.size(); i++) {
            Question question = (Question) questionList.get(i);
            List questionMetasList = QuestionMetas.dao.getQuestionMetas(Long.valueOf(question.getBigInteger("id").toString()));
            for (int j = 0; j < questionMetasList.size(); j++) {
                QuestionMetas questionMetas = (QuestionMetas) questionMetasList.get(j);
                question.set("item" + (j + 1) + "_id", questionMetas.get("options"));
            }
            testList.add(question);
        }
        List nullList = new ArrayList();
        Map<String, Object> questionsMap = new HashMap<String, Object>();
        questionsMap.put("question", questionList);
        questionsMap.put("answered", nullList);
        dataMapList.put("questions", questionsMap);
        // 题目保存
        Testpaper.dao.saveTestPaper(test, questionList, studentId);
        return dataMapList;
    }

    /**
     * 判断考试次数公共类
     * @param studentId
     * @param targetId
     * @param targetType
     * @return
     */
    public static boolean isTwiceExam(Long studentId, Long targetId, String targetType) {
        Long examCount = TestpaperResult.dao.getTestpaperCount(studentId, targetId, targetType, 1, -1);
        Exam exam = Exam.dao.findById(targetId);
        if (exam.getInt("times") == 0) {
            return true;
        } else {
            if (examCount >= exam.getInt("times")) {
                return false;
            } else {
                return true;
            }
        }
    }
}
